const Article = require("../model/article.model");
const User = require("../model/user.model");
const ArticleUser = require("../model/articleUser.model");
class ArticleService {
  // 创建文章
  async createArticle(
    articleContent,
    articleTitle,
    userId,
    articleType,
    articlePreView
  ) {
    const data = {
      articleTitle,
      articleContent,
      articleType,
      userId,
      articlePreView,
      lookNum: 0,
    };
    const res = await Article.create(data);
    return res.dataValues;
  }
  // 查询文章
  async selectArticle(type, curentPage, pageSize) {
    const data = type ? { articleType: type } : {};
    const { count, rows } = await Article.findAndCountAll({
      where: data,
      offset: (curentPage - 1) * pageSize, // 查询的起始下标
      limit: pageSize, // 查询的条数
      order: [
        //并针对有效方向列表进行降序排列
        ["createdAt", "DESC"],
      ],
    });

    return {
      rows,
      count,
    };
  }
  //获取面经详情
  async articleDetail(id) {
    const res0 = await Article.findOne({
      where: { id },
    });
    await Article.update({ lookNum: res0.lookNum + 1 }, { where: { id } });
    const res = await Article.findOne({
      where: { id },
    });
    const creater = await User.findOne({
      where: { id: res.dataValues.userId },
    });
    const { userEmail, userName } = creater;
    const result = {
      ...res.dataValues,
      userEmail,
      userName,
    };
    return result ? result : null;
  }
  // 收藏文章
  async createCollect(articleId, userId) {
    const res = await ArticleUser.create({ articleId, userId });
    return res;
  }
  //是否收藏
  async isCollect(articleId, userId) {
    const res = await ArticleUser.findOne({ where: { articleId, userId } });
    return res ? res : null;
  }
  //取消收藏
  async deleteCollect(articleId, userId) {
    const res = await ArticleUser.destroy({ where: { articleId, userId } });
    return res ? res : null;
  }
  //获取我的发布列表
  async getMyArticle(userId, curentPage, pageSize) {
    const { count, rows } = await Article.findAndCountAll({
      where: { userId },
      offset: (curentPage - 1) * pageSize, // 查询的起始下标
      limit: pageSize, // 查询的条数
      order: [
        //并针对有效方向列表进行降序排列
        ["createdAt", "DESC"],
      ],
    });
    return {
      count,
      rows,
    };
  }
  //查看收藏面经列表
  async getCollectArticleList(userId, curentPage, pageSize) {
    const { count, rows } = await ArticleUser.findAndCountAll({
      where: { userId },
      offset: (curentPage - 1) * pageSize, // 查询的起始下标
      limit: pageSize, // 查询的条数
      order: [
        //并针对有效方向列表进行降序排列
        ["createdAt", "DESC"],
      ],
    });
    const articleIdArr = await Promise.all(
      rows.map(
        async (item) =>
          await Article.findOne({
            where: { id: item.articleId },
          })
      )
    );
    return {
      count,
      articleIdArr,
    };
  }
  // 获取热门文章
  async hotArticle() {
    let res = await Article.findAll({
      order: [["lookNum", "DESC"]],
    });
    res = res.slice(0, 6);
    return res;
  }
  // 删除文章
  async deleteTheArticle(id) {
    const res = Article.destroy({
      where: { id } 
    });
    return res;
  }
}

module.exports = new ArticleService();
